<?php
namespace Home\Controller;
use Think\Controller;
class ShareController extends Controller {
    public function shareZhuan($getCsType=null){//分享赚
        if(cookie('jploginHC')==''){
            cnzz_pvuv('shareZhuan');//执行统计
            cookie('jploginHC',1,10);
        }

        if($_REQUEST['csType']){
            $getCsType=$_REQUEST['csType'];
        }
        if($_REQUEST['uid']){
            cookie('uid',$_REQUEST['uid']);
        }
        $uid=cookie('uid');
        if(empty($uid)){
            $uid=date('Ymd').mt_rand(10000,99999).date('His');
            cookie('uid',$uid);
        }
        $toDay=date('Y-m-d');
        $first=1;
        $w=date('w',strtotime($toDay));
        $weekStart=date('Y-m-d',strtotime("$toDay -".($w ? $w - $first : 6).' days'));//本周开始日期
        $weekEnd=date('Y.m.d',strtotime("$weekStart +6 days"));  //本周结束日期

        $userArr=M()->query("select * from tb_zhuan_share_user where uid='{$uid}' limit 1");
        if(empty($userArr)){
            M()->query("insert into tb_zhuan_share_user (uid,bzjp,refreshTime)values('{$uid}',1,'{$toDay}')");
            $userArr=M()->query("select * from tb_zhuan_share_user where uid='{$uid}' limit 1");
        }else{
           $refreshTime=$userArr[0]['refreshTime'];
            if(strtotime($refreshTime)<strtotime($weekStart)){
                M()->query("update tb_zhuan_share_user set refreshTime='{$toDay}',bzjp=1 where id='{$userArr[0]['id']}'");
                $userArr=M()->query("select * from tb_zhuan_share_user where uid='{$uid}' limit 1");
            }
        }
        S('tb_zhuan_share_user'.$uid,$userArr,2592000);//缓存数据

        $priceArr=M()->query("select * from tb_zhuan_share_price where id>=1 and switch=1");
        foreach($priceArr as $key=>$value){
            if(stristr($userArr[0]['paidCs'],$value['csType'])){//有优惠
                $priceArr[$key]['discount']=0;
                cookie('pricezhuan',null);//没有优惠价格
            }else{
                $priceArr[$key]['price']=$value['price']-$value['discount'];
            }
            $csNumber=round($value['csNumber']/10000,1);
            $priceArr[$key]['csNumber']=$csNumber.'万';

            $priceArr[$key]['bouns']=round($priceArr[$key]['price']*0.4,1);
            if(mb_substr($value['csType'],0,2)=='jc'){
                $priceArr[$key]['jumpUrl']=$value['jumpUrl']."&price={$priceArr[$key]['price']}&discount={$priceArr[$key]['discount']}&csType={$value['csType']}&uid={$uid}";
            }else{
                $priceArr[$key]['jumpUrl']=$value['jumpUrl']."?price={$priceArr[$key]['price']}&discount={$priceArr[$key]['discount']}&csType={$value['csType']}&uid={$uid}";
            }

            $priceArr[$key]['weekStart']=str_replace('-','.',$weekStart);
            $priceArr[$key]['weekEnd']=$weekEnd;

            $collection=$this->selectCollection($uid,$getCsType);//是否收藏
            $priceArr[$key]['collection']=$collection;

            if($getCsType==$value['csType']){//其他页面返回
                if(cookie('jploginHC')==''){
                   $shraeArr= M()->query("select id,csNumber from tb_zhuan_share_price where csType='{$value['csType']}'");
                    M()->query("update tb_zhuan_share_price set csNumber='{$shraeArr[0]['csNumber']}'+1 where csType='{$value['csType']}'");
                    cookie('jploginHC',1,30);
                }

                if(mb_substr($value['csType'],0,2)=='jc'){
                    echo json_encode($priceArr[$key]);
                }else{
                    return $priceArr[$key];
                }
             exit;
            }
        }

        $this->assign('priceArr',$priceArr);
        $this->display("Share/shareZhuan");
    }
    public function addCollection(){//添加收藏
        if($_REQUEST['uid']){$uid=$_REQUEST['uid'];
        }else{$uid=cookie('uid');
        }
        if($_REQUEST['csType']){
            $did=$_REQUEST['csType'];
        }else{
            $did=cookie('csType');
        }
        $csTypeArr=M()->query("select id from tb_zhuan_share_price where csType='{$did}'");
        $collectionArr=M()->query("select id from tb_zhuan_share_collection where uid='{$uid}' and did='{$csTypeArr[0]['id']}'");
        if(empty($collectionArr)){
            M()->query("insert into tb_zhuan_share_collection (uid,did)values('{$uid}','{$csTypeArr[0]['id']}')");
        }
        echo 1;
    }
    public function deleteCollection(){//删除收藏
        if($_REQUEST['uid']){$uid=$_REQUEST['uid'];
        }else{$uid=cookie('uid');
        }
        if($_REQUEST['csType']){
            $did=$_REQUEST['csType'];
        }else{
            $did=cookie('csType');
        }

        $csTypeArr=M()->query("select id from tb_zhuan_share_price where csType='{$did}'");
        M()->query("delete from tb_zhuan_share_collection where uid='{$uid}' and did='{$csTypeArr[0]['id']}'");
        echo 1;
    }
    public function selectCollection($uid=null,$did=null){//是否收藏
        if($_REQUEST['uid']){$uid=$_REQUEST['uid'];
        }else if(empty($uid)){$uid=cookie('uid');
        }
        if($_REQUEST['csType']){
            $did=$_REQUEST['csType'];
        }else if(empty($did)){
            $did=cookie('csType');
        }
        $csTypeArr=M()->query("select id from tb_zhuan_share_price where csType='{$did}'");
        $collectionArr=M()->query("select id from tb_zhuan_share_collection where uid='{$uid}' and did='{$csTypeArr[0]['id']}'");
        if($collectionArr[0]['id']){
            return 1;
        }else{
            return 0;
        }
    }

    public function adddivideinto($uid,$ordernum,$price){//分享支付回调
        $price=round($price*0.4,1);
        $uid=mb_substr($uid,5,strlen($uid));
        $url = "http://api.ddznzj.com/adddivideinto?uid={$uid}&ordernum={$ordernum}&price={$price}";

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
        $json = curl_exec($ch);
    }

    public function shareCode(){//分享二维码
        if($_REQUEST['uid']){
            $uid=$_REQUEST['uid'];
        }else{
            $uid=cookie('uid');
        }
        $csName=$_REQUEST['csName'];
        if($_REQUEST['csType']){
            $csName=$_REQUEST['csType'];
        }

        $zhuanArr=M()->query("select * from tb_zhuan_share_price where csType='{$csName}'");
        $discount=$zhuanArr[0]['discount'];
        $Name=$zhuanArr[0]['csName'];
        $Dprice=$zhuanArr[0]['price']-$zhuanArr[0]['discount'];
        $imgTitleArr=explode('-',$zhuanArr[0]['img_title']);

        $Name1=$imgTitleArr[0];
        $Name2=$imgTitleArr[1];
        $Name1Len=mb_strlen($Name1,'utf-8');

        $toDay=date('Y-m-d');
        $first=1;
        $w=date('w',strtotime($toDay));
        $weekStart=date('Y-m-d',strtotime("$toDay -".($w ? $w - $first : 6).' days'));//本周开始日期
        $weekEnd=date('Y.m.d',strtotime("$weekStart +6 days"));  //本周结束日期
        $weekEnd=explode('.',$weekEnd);
        $validity="有效期至：{$weekEnd[0]}年{$weekEnd[1]}月{$weekEnd[2]}日";

        if(mb_substr($csName,0,2)=='jc'){//轻测
            $typeid=substr($csName,2,2);
            $url="https://hy.yixueqm.com/jianceH5/test.html?typeid={$typeid}&shareUid={$uid}&csType={$csName}&price={$Dprice}&channel=qudao100";
            $size=1.8;
        }else{
            $url="https://hy.yixueqm.com/zhiming/index.php/Home-{$csName}-index?shareUid={$uid}&csType={$csName}&price={$Dprice}&channel=qudao100";
            $size=1.7;
        }

        $codeClass=new \Library\QRcode();
        $value =$url;         //二维码内容
        $errorCorrectionLevel = 'Q';  //容错级别  默认为L，这个参数可传递的值分别是L(QR_ECLEVEL_L，7%)、M(QR_ECLEVEL_M，15%)、Q(QR_ECLEVEL_Q，25%)、H(QR_ECLEVEL_H，30%)
        $matrixPointSize = $size;      //生成图片大小
        //生成二维码图片
        $filename = 'Upload/qrcode/qrCode.png';
        $codeClass->png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
        $QR = $filename;        //已经生成的原始二维码图片文件
        //$QR = imagecreatefromstring(file_get_contents($QR));
        //输出图片
        //imagepng($QR, 'Upload/qrcode/qrcode.png');
        // imagedestroy($QR);
        //echo '<img src="qrcode.png" alt="使用微信扫描支付">';


        // 图片一
        $path_1 = mb_substr(THINK_PATH,0,-9)."Public/images/qrcode/fxzq_banner.png";
        // 图片二
        $path_2 = mb_substr(THINK_PATH,0,-9).$filename;
        // 创建图片对象
        $image_1 = imagecreatefrompng($path_1);
        $image_2 = imagecreatefrompng($path_2);
        // 合成图片
        imagecopymerge($image_1, $image_2, 377,135, 0, 0, imagesx($image_2), imagesy($image_2), 100);
        // 添加文字水印
        $black = imagecolorallocate($image_1, 50, 50, 50);
        $red=imagecolorallocate($image_1, 198, 20, 49);
        $white=imagecolorallocate($image_1, 253, 233, 220);
        $white2=imagecolorallocate($image_1, 240, 240, 240);
        $font = mb_substr(THINK_PATH,0,-9).'Upload/font/simhei.ttf';

        imagettftext($image_1, 20, 0, 100, 50, $black, $font, "送您一个专属的优惠红包，");

        imagettftext($image_1, 20, 0, 50, 100, $black, $font, $Name1);
        imagettftext($image_1, 22, 0, $Name1Len*27+50, 100, $red, $font, $Name2);

        imagettftext($image_1, 65, 0, 150, 200, $white, $font, $discount);
        imagettftext($image_1, 22, 0, 240, 195, $white, $font, '元');
        imagettftext($image_1, 18, 0, 50, 225, $white2, $font, $validity);
        imagettftext($image_1, 18, 0, 90, imagesy($image_1)-20, $black, $font, "扫描上方二维码，赶快过来领钱");

        $new_file = mb_substr(THINK_PATH,0,-9).'Upload/'.date("Y-m").'/';
        if(!is_dir($new_file)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file);
            chmod($new_file,0777);
        }
        // 输出合成图片
        $returnPath=$new_file.$uid.$csName.'qrCode.png';
        $imgUrl="https://{$_SERVER['HTTP_HOST']}".__ROOT__.'/Upload/'.date("Y-m").'/'.$uid.$csName.'qrCode.png';

        $boole=imagepng($image_1,$returnPath);
        if($boole){
            //$priceArr=M()->query("select id,csName,img from tb_zhuan_share_price where csType='{$csName}'");
            $arr=array(
                'url'=>$url,
                'shareimg'=>$imgUrl,
                'sharetitle'=>$zhuanArr[0]['text'],
                'shareicon'=>$zhuanArr[0]['img'],
            );
            echo json_encode($arr);
        }else{
            echo '合成失败';
        }
    }

}